home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995 August: Tool Chest / Dev.CD Aug 95 TC / Dev.CD Aug 95 TC.toast / Tool Chest / Development Tools & Languages / HyperCard Related / APDA HyperCard Toolkits / HyperCard CTB Toolkit 1.0b2 / Source Code / CTBRecvChars.p < prev    next >
Encoding:
Text File  |  1995-02-07  |  2.8 KB  |  110 lines  |  [TEXT/MPS ]

  1. (*
  2.     CTBRecvChars([n[,dontStrip]]) -- Receive n characters, waiting until they're available if necessary.
  3.         Default to all available characters. If the second parameter is present, then don't do control
  4.         character and top bit stripping.
  5.  
  6.     To compile and link this file using Macintosh Programmer's Workshop,
  7.  
  8.         pascal -w CTBRecvChars.p
  9.         link -m ENTRYPOINT -o HyperCommands -rt XFCN=2752 -sn Main=CTBRecvChars ∂
  10.             CTBRecvChars.p.o "{MPW}"Libraries:interface.o "{MPW}"Libraries:Libraries:HyperXLib.o
  11.  
  12.     © Copyright 1990 by Apple Computer, Inc.
  13.  
  14.     Initial coding 2/90 by Harry R. Chesley.
  15. *)
  16.  
  17. {$R-}
  18.  
  19. {$S CTBRecvChars }     { Segment name must be the same as the command name. }
  20.  
  21. unit DummyUnit;
  22.  
  23. interface
  24.  
  25. uses MemTypes, QuickDraw, OSIntf, ToolIntf, CTBUtils, FTIntf, CMIntf, TMIntf, CRMIntf, HyperXCmd;
  26.  
  27. procedure EntryPoint(paramPtr: XCmdPtr);
  28.     
  29. implementation
  30.  
  31. procedure CTBRecvChars(paramPtr: XCmdPtr); forward;
  32.  
  33. procedure EntryPoint(paramPtr: XCmdPtr);
  34.  
  35.     begin
  36.         CTBRecvChars(paramPtr);
  37.     end;
  38.  
  39. procedure CTBRecvChars(paramPtr: XCmdPtr);
  40.  
  41.     {$I CTBUtil.inc}
  42.  
  43.     var toRead, haveRead: longInt;
  44.         l: longInt;
  45.         h: Handle;
  46.         p: Ptr;
  47.         theBuf: InputBufferHandle;
  48.         sizes: CMBufferSizes;
  49.         status: CMStatFlags;
  50.  
  51.     procedure Fail(errMsg: Str255); { set theResult and quit }
  52.         begin
  53.             paramPtr^.returnValue := PasToZero(paramPtr,errMsg);
  54.             exit(CTBRecvChars);
  55.         end;
  56.  
  57.     begin
  58.         { Check the parameter count. }
  59.         if paramPtr^.paramCount > 2 then Fail('Invalid parameter count');
  60.  
  61.         { Check that the Comm Toolbox is ready. }
  62.         CTBReady;
  63.         { And that we have a connection tool. }
  64.         EnsurePresent(connectionTool);
  65.         { And that it's open. }
  66.         EnsureOpen;
  67.         { Get the input buffer. }
  68.         theBuf := InputBufferHandle(CMGetUserData(Globals^^.connHand));
  69.         { Drop any old termination information. }
  70.         if theBuf^^.termString <> nil then
  71.             begin
  72.                 DisposHandle(theBuf^^.termString);
  73.                 theBuf^^.termString := nil;
  74.                 theBuf^^.timeOut := -1;
  75.             end;
  76.  
  77.         { Figure out how much to read. }
  78.         if ParmPresent(1) then toRead := GetLongParm(1)
  79.         else if CMStatus(Globals^^.connHand,sizes,status) = noErr then
  80.             toRead := sizes[cmDataIn]+theBuf^^.amountLeft
  81.         else toRead:= theBuf^^.amountLeft;
  82.  
  83.         { Allocate a handle for it. }
  84.         h := NewHandle(toRead);
  85.         if h = nil then Fail('Out of memory');
  86.         haveRead := 0;
  87.         if toRead > 0 then
  88.             begin
  89.                 HLock(h);
  90.                 { First get the stuff in the input buffer. }
  91.                 haveRead := min(toRead,theBuf^^.amountLeft);
  92.                 if haveRead > 0 then
  93.                     begin
  94.                         BlockMove(theBuf^^.bufferPtr,h^,haveRead);
  95.                         theBuf^^.amountLeft := theBuf^^.amountLeft - haveRead;
  96.                         toRead := toRead - haveRead;
  97.                     end;
  98.                 { Then go to the outside for the rest. }
  99.                 if toRead > 0 then haveRead := haveRead + ReadFromConn(Ptr(ord4(h^)+haveRead),toRead);
  100.                 HUnlock(h);
  101.             end;
  102.  
  103.         { Strip and return the results. }
  104.         StripBytes(h,haveRead,not ParmPresent(2));
  105.  
  106.         paramPtr^.returnValue := h;
  107.     end;
  108.  
  109. end.
  110.